<template>
  <div class="container">
    <div class="merchant">
      <!-- 商家搜索 -->
      <div class="merchant_top">
        <img
          src="@/assets/img/login/nav_ic_back_dark@2x.png"
          alt=""
          style="width:44px;margin-right: 10px;"
          @click="$router.back()"
        />
        <el-input
          v-model="params.search"
          clearable
          :placeholder="$t('login_search')"
          prefix-icon="el-icon-search"
          style="margin-right: 14px;"
          @clear="onRefresh"
          @keyup.enter.native="keyConfirm"
        ></el-input>
      </div>
      <!-- 商家tab -->
      <div class="merchant_tab">
        <div
          :class="{ tab_active: params.type == item.value }"
          v-for="item in tabList"
          :key="item.value"
        >
          <span @click="changeValue(item.value)">{{ item.label }}</span>
        </div>
      </div>
      <!-- 商家列表 -->

      <van-pull-refresh
        v-model="refreshing"
        @refresh="onRefresh"
        :loading-text="$t('Loading')"
        :pulling-text="$t('Loading')"
        :loosing-text="$t('Loading')"
      >
        <van-list
          v-model="loading"
          :finished="finished"
          :loading-text="$t('Loading')"
          :finished-text="
            shopList.length == 0 ? $t('error_empty') : $t('No_more')
          "
          @load="onLoad"
        >
          <div
            class="merchant_list"
            @click="toDetail(item)"
            v-for="item in shopList"
            :key="item.id"
          >
            <div style="margin-right:12px">
              <img
                :src="imgUrl + item.logo"
                alt=""
                style="width: 80px;height: 80px;border-radius: 7px;"
              />
            </div>
            <div class="merchant_list_right">
              <div class="merchant_name">{{ item.branchName }}</div>
              <div class="merchant_powerBank" v-if="item.isBusiness">
                {{ $t('home_rent') }}:
                <span style="margin-right:10px;">{{ item.rent }}</span>
                {{ $t('home_return') }}:
                <span>{{ item.recovery }}</span>
              </div>
              <div class="merchant_powerBank" v-else>
                {{ $t('home_outOfService') }}
              </div>
              <div class="merchant_address">
                {{ item.address }}
              </div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import share from '@/mixins/share'
import $api from '@/assets/js/api/merchant'
export default {
  mixins: [share],
  data () {
    return {
      params: {
        lat: 38.9958641, // 纬度
        lng: -76.909393, // 经度
        offset: 0, //	偏移量（开始值：0）
        search: '', //	查询内容（网点名称||网点地址）
        type: 1 //	网点类型：null-全部 1-可租借 2-可归还
      },
      tabList: [],
      shopList: [],
      loading: false,
      finished: false,
      refreshing: false,
      imgUrl: ''
    }
  },
  computed: {},
  created () {
    this.getLang()
    this.tabList = [
      {
        value: 1,
        label: this.$t('home_rent')
      },
      {
        value: 2,
        label: this.$t('home_return')
      },
      {
        value: null,
        label: this.$t('home_all')
      }
    ]
    let fileHost = JSON.parse(localStorage.getItem('configs')).find(
      item => item.key == 'file-host'
    )
    if (!fileHost) this.imgUrl = process.env.BASE_URL + '/'
    else this.imgUrl = fileHost.value
  },
  mounted () {
    // this.getLocation()
  },
  methods: {
    keyConfirm (e) {
      if (e.keyCode == 13) this.onRefresh()
    },
    //选择商铺状态
    changeValue (e) {
      this.params.type = e
      this.onRefresh()
    },

    //获取定位
    getLocation () {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
          this.showPosition,
          this.showError
        )
      } else {
        console.log('Geolocation is not supported by this browser.')
      }
    },

    //获取经纬度
    showPosition (position) {
      this.params.lat = position.coords.latitude
      this.params.lng = position.coords.longitude
    },

    //获取失败
    showError (error) {
      switch (error.code) {
        case error.PERMISSION_DENIED:
          console.log('User denied the request for Geolocation.')
          break
        case error.POSITION_UNAVAILABLE:
          console.log('Location information is unavailable.')
          break
        case error.TIMEOUT:
          console.log('The request to get user location timed out.')
          break
        case error.UNKNOWN_ERROR:
          console.log('An unknown error occurred.')
          break
      }
    },

    onLoad () {
      setTimeout(() => {
        this.getBranches()
      }, 1000)
    },

    onRefresh () {
      this.finished = false
      this.shopList = []
      this.params.offset = 0
      this.loading = true
      this.onLoad()
    },

    //获取网点
    getBranches () {
      if (this.refreshing) {
        this.refreshing = false
      }
      $api.getBranches(this.params, true).then(res => {
        if (res.code == 0) {
          this.shopList = this.shopList.concat(res.data)
          this.params.offset = this.shopList.length
          if (res.data.length < 20) {
            this.finished = true
          }
        }
        this.loading = false
      })
    },

    toDetail (item) {
      let params = {
        id: item.id,
        lat: item.lat,
        lng: item.lng
      }
      this.$router.push({
        path: '/merchant/detail',
        query: { params: JSON.stringify(params) }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.merchant {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background: #f5f5f7;
  .merchant_top {
    display: flex;
    align-items: center;
    height: 44px;
    position: fixed;
    z-index: 9;
    width: 100%;
    background: #ffffff;
    ::v-deep.el-input__inner {
      height: 32px !important;
      background: #f0f0f2;
      border-radius: 16px;
      font-size: 12px;
      line-height: 32px !important;
      color: #39304d;
      border: none;
    }
    ::v-deep .el-input__inner::placeholder {
      color: #888888;
    }
    ::v-deep.el-input__icon {
      line-height: 32px !important;
      height: 32px !important;
      width: 12px !important;
      display: block;
      color: #888888;
    }
    ::v-deep.el-input__prefix {
      left: 8px !important;
    }
    ::v-deep.el-input__suffix {
      right: 14px !important;
    }
  }
  .merchant_tab {
    display: flex;
    text-align: center;
    width: calc(100% - 28px);
    margin: 54px 0 10px 14px;
    height: 48px;
    background: #ececee;
    border-radius: 5px;
    div {
      flex: 1;
      height: 40px;
      border-radius: 10px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      margin: 4px;
      text-align: center;
      color: #aaaaac;
      font-weight: 600;
      line-height: 16px;
    }
    .tab_active {
      font-weight: 700;
      color: var(--text-color);
      background: #ffffff;
    }
  }
  .merchant_list {
    width: calc(100% - 28px);
    margin: 0 14px 10px 14px;
    background: #ffffff;
    padding: 12px 16px 12px;
    border-radius: 14px;
    display: flex;
    align-items: start;
    .merchant_list_right {
      width: calc(100% - 92px);
      margin: 2px 0 0 0;
      .merchant_name {
        font-size: 15px;
        font-weight: 600;
        text-align: left;
        color: #39404d;
        line-height: 18px;
        white-space: nowrap; /* 不换行 */
        overflow: hidden; /* 控制超出部分的处理方式 */
        text-overflow: ellipsis; /* 使用省略号表示被裁切的部分 */
      }
      .merchant_powerBank {
        margin: 6px 0;
        font-size: 11px;
        font-weight: 400;
        text-align: left;
        color: var(--text-color);
        line-height: 13px;
        span {
          font-size: 17px;
          font-weight: 600;
        }
      }
      .merchant_address {
        font-size: 12px;
        font-weight: 400;
        text-align: left;
        color: #888888;
        line-height: 14px;
        margin-right: 12px;
        display: -webkit-box; /* 必须指定display属性 */
        overflow: hidden; /* 控制超出部分的处理方式 */
        text-overflow: ellipsis; /* 使用省略号表示被裁切的部分 */
        -webkit-line-clamp: 2; /* 最多显示的行数 */
        -webkit-box-orient: vertical; /* 必须指定box-orient属性 */
      }
    }
  }
}
</style>
